package service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import dao.AccountDAO;
import dao.OrderDAO;
import dao.OrderGoodsDAO;
import po.Account;
import po.Order;
import po.OrderGoods;
import service.ICustomer;
@Service("icustomer")
public class ICustomerImpl implements ICustomer {
	@Autowired(required=true)
	private OrderDAO od;
	@Autowired(required=true)
	private OrderGoodsDAO odg;
	@Autowired(required=true)
	private OrderGoodsDAO ogd;
	@Autowired(required=true)
	private AccountDAO ad;
	
	@Override
	public int addToCart(String goodsID) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteFromCart(String goodsID) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int queryCart() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int checkout(String[] goodsID) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int cancelOrder(String orderID) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public  List<Order> queryOrders(String cid) {
		// TODO Auto-generated method stub
		System.out.println("queryOrders from db cid");
		List<Order> myOrder = od.listOrdersByCid(cid);
		for(int i=0;i<myOrder.size();i++){
			List<OrderGoods> lsog = ogd.findOrder(myOrder.get(i).getOid());
			Map<String, Integer> goodsIDs = new HashMap<>();
			for(int j=0;j<lsog.size();j++)
				goodsIDs.put(lsog.get(j).getGid(), lsog.get(j).getNum());
			myOrder.get(i).setGoodsIDs(goodsIDs );
		}
		return myOrder;
	}

	@Override
	public int deleteOrder(String orderID) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int signUp(Account acc) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	@Transactional
	public int makeOrder(Order order) {
		// TODO Auto-generated method stub
		System.out.println("make order to db");
		od.addOrders(order);
		Map<String, Integer> map  = order.getGoodsIDs();
		for(Entry<String, Integer> entry:map.entrySet()){   
			
			OrderGoods og = new OrderGoods();
			og.setOid(order.getOid());
			og.setGid(entry.getKey());
			og.setNum(entry.getValue());
			odg.addGoods(og);
	
		} 
		return 0;
	}

	@Override
	public Account loginIn(Account acc) {
		// TODO Auto-generated method stub
		 return ad.getAccountId(acc.getAccount(), acc.getPsd(), acc.getIdentity());
	}

}
